# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
 
class LoginDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.setWindowTitle(u'登录')
        self.resize(300, 150)
 
        self.leName = QtGui.QLineEdit(self)
        self.leName.setPlaceholderText(u'用户名')
 
        self.lePassword = QtGui.QLineEdit(self)
        self.lePassword.setEchoMode(QtGui.QLineEdit.Password)
        self.lePassword.setPlaceholderText(u'密码')
 
        self.pbLogin = QtGui.QPushButton(u'登录', self)
        self.pbCancel = QtGui.QPushButton(u'取消', self)
 
        self.pbLogin.clicked.connect(self.login)
        self.pbCancel.clicked.connect(self.reject)
 
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.leName)
        layout.addWidget(self.lePassword)
 
        # 放一个间隔对象美化布局
        spacerItem = QtGui.QSpacerItem(20, 48, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        layout.addItem(spacerItem)
 
        # 按钮布局
        buttonLayout = QtGui.QHBoxLayout()
        # 左侧放一个间隔
        spancerItem2 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        buttonLayout.addItem(spancerItem2)
        buttonLayout.addWidget(self.pbLogin)
        buttonLayout.addWidget(self.pbCancel)
 
        layout.addLayout(buttonLayout)
 
        self.setLayout(layout)
 
    def login(self):
        print 'login'
        if self.leName.text() == 'admin' and self.lePassword.text() == 'admin':
            self.accept() # 关闭对话框并返回1
        else:
            QtGui.QMessageBox.critical(self, u'错误', u'用户名密码不匹配')
 
 
def login():
    """返回True或False"""
    dialog = LoginDialog()
    if dialog.exec_():
        return True
    else:
        return False
 
if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    if login():
        mainWindow = QtGui.QMainWindow()
        mainWindow.show()
        sys.exit(app.exec_())
